package posecco.client;

import posecco.shared.*;

import com.google.gwt.dom.client.Element;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Navigation extends FlowPanel {

	final String[] links = {
			
			"login",
			"registration",
			"userList",
			"RFCEdit"
	};
	
	public Navigation() {

        this.setStyleName("navList");
        for (int i = 0; i < links.length; ++i) {
        	
            Hyperlink tmp = new Hyperlink(Util.getMessage(links[i] + "ButtonLabel"), links[i]);
            tmp.getElement().setId(links[i] + "Link");
            tmp.setStyleName("navItem");
            add(tmp);
        }

		History.addValueChangeHandler(new ValueChangeHandler<String>() {
			public void onValueChange(ValueChangeEvent<String> event) {
				changePage();
			}
		});
 
	}
	
	public static void gotoPage(String page) {
		
		History.newItem(page, true);
	}
	
	public void changePage() {
		
        for (int i = 0; i < links.length; ++i) {
        	
        	Element link = DOM.getElementById(links[i] + "Link");
            link.removeClassName("active");
            if (link.getId().toLowerCase().equals((History.getToken() + "Link").toLowerCase())) {
            	
            	link.addClassName("active");
            }
        }

	    if(History.getToken().equals("login")) {
		       RootPanel.get("content").clear();
		       RootPanel.get("content").add(new Login());
	    } else if (History.getToken().equals("registration")) {
		       RootPanel.get("content").clear();
		       RootPanel.get("content").add(new Registration());
	    } else if (History.getToken().indexOf("editUser") > -1) {
		       RootPanel.get("content").clear();
		       RootPanel.get("content").add(new Registration(Long.parseLong(History.getToken().replace("editUser", ""), 10)));
	    } else if (History.getToken().equals("RFCEdit")) {
	    	   RootPanel.get("content").clear();
	    	   RootPanel.get("content").add(new RFCEdit());
	    } else {
		       RootPanel.get("content").clear();
		       RootPanel.get("content").add(new UserList());
	    }
	}
}